// smartclass/pages/adddormitoryManagementt/adddormitoryManagementt.js
const _wx = require('../../../utils/util.js');
import Toast from '@vant/weapp/toast/toast';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    fools: '',
    sex: "",
    fieldValue: "",
    fieldValues: "",
    fieldValue2: "",
    fieldValue2s: "",
    fieldValue3: "",
    fieldValue3s: "",
    fieldValue4: "",
    fieldValue4s: "",
    username: "",
    showPicker: false,
    showPicker2: false,
    showPicker3: false,
    showPicker4: false,
    radio2: "1",
    columns: [],
    columns2: [],
    columns2s: [],
    columns3: [],
    columns4: [{
        text: "学生",
        value: 1
      },
      {
        text: "教师",
        value: 2
      },
    ],
  },
  notification() {
    this.setData({
      showPicker: true
    })
  },
  notification2() {
    this.setData({
      showPicker2: true
    })
  },
  notification3() {
    this.setData({
      showPicker3: true
    })
  },
  notification4() {
    this.setData({
      showPicker4: true
    })
  },
  onCancel() {
    this.setData({
      showPicker: false
    })
  },
  onCancel2() {
    this.setData({
      showPicker2: false
    })
  },
  onCancel3() {
    this.setData({
      showPicker3: false
    })
  },
  onCancel4() {
    this.setData({
      showPicker4: false
    })
  },
  onConfirm(selectedOptions) {
    this.setData({
      columns2: []
    })
    this.setData({
      columns2s: []
    })
    this.setData({
      fieldValue2: ''
    })
    const that = this
    console.log(selectedOptions.detail.value.fools);
    this.setData({
      fools: selectedOptions.detail.value.fools
    })
    for (let index = 1; index <= this.data.fools; index++) {
      that.data.columns2.push(index)
      this.setData({
        columns2: that.data.columns2
      })
    }
    console.log(this.data.columns2);
    this.data.columns2.forEach((element) => {
      this.data.columns2s.push({
        text: element + "层",
        value: element
      })

    })
    this.setData({
      columns2s: that.data.columns2s
    })
    this.setData({
      showPicker: false
    })
    this.setData({
      fieldValue: selectedOptions.detail.value.text
    })
    this.setData({
      fieldValues: selectedOptions.detail.value.value
    })
    if (selectedOptions.detail.value.sex == 1) {
      this.setData({
        sex: "男"
      })
    } else {
      this.setData({
        sex: "女"
      })
    }

  },
  onConfirm2(selectedOptions) {
    this.setData({
      showPicker2: false
    })
    this.setData({
      fieldValue2: selectedOptions.detail.value.text
    })
    this.setData({
      fieldValue2s: selectedOptions.detail.value.value
    })
  },
  onConfirm3(selectedOptions) {
    this.setData({
      showPicker3: false
    })
    this.setData({
      fieldValue3: selectedOptions.detail.value.text
    })
    this.setData({
      fieldValue3s: selectedOptions.detail.value.value
    })
  },
  onConfirm4(selectedOptions) {
    this.setData({
      showPicker4: false
    })
    this.setData({
      fieldValue4: selectedOptions.detail.value.text
    })
    this.setData({
      fieldValue4s: selectedOptions.detail.value.value
    })
  },
  // 所属楼栋
  dormdormBuildings() {
    const that = this
    _wx.getData('admin/dorm/noneedauth/dormBuildings', 'GET', {

    }, function (res) {
      if (res.code == 0) {
        res.data.forEach(element => {
          that.data.columns.push({
            text: element.name,
            value: element.id,
            sex: element.type,
            fools: element.floor_total
          })
          that.setData({
            columns: that.data.columns
          })
        });

      }
    });
  },
  // 获取所有宿舍类型
  dormnoneedauthdormType() {
    const that = this
    _wx.getData('admin/dorm/noneedauth/dormType', 'GET', {

    }, function (res) {
      if (res.code == 0) {
        console.log(res.data);
        res.data.forEach(element => {
          that.data.columns3.push({
            text: element.name,
            value: element.id,
          })
          that.setData({
            columns3: that.data.columns3
          })
        });
      }
    });
  },
  // 添加接口
  dormdorminsert() {
    const that = this
    _wx.getData('admin/dorm/dorm/insert', 'POST', {
      building_id: this.data.fieldValues,
      floor: this.data.fieldValue2s,
      name: this.data.username,
      bed_num: this.data.bednum,
      stay_sex: this.data.sex == "男" ? 1 : 2,
      identity: this.data.fieldValue4s,
      dorm_type: this.data.fieldValue3s,
      status: this.data.radio2
    }, function (res) {
      if (res.code == 0) {
        Toast.success('添加成功');
        setTimeout(function () {
          wx.redirectTo({
            url: '../dormitoryManagementt/dormitoryManagementt',
          })
        }, 2000);
      }
    });
  },
  // 提交按钮
  onsubmit() {
    this.dormdorminsert()
  },
  onChange(event) {
    this.setData({
      bednum: (event.detail)
    })
  },
  onChangese(event) {
    this.setData({
      radio2: (event.detail)
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    // 获取所有楼栋信息
    this.dormdormBuildings()
    // 获取所有宿舍类型
    this.dormnoneedauthdormType()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})